
package com.jf.cloud.product.controller.multishop;

import com.alibaba.excel.EasyExcel;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.util.ExcelUtil;
import com.jf.cloud.product.dto.StockBillLogDTO;
import com.jf.cloud.product.listener.StockBillLogItemExcelListener;
import com.jf.cloud.product.service.StockBillLogItemService;
import com.jf.cloud.product.vo.StockBillLogItemExcelVO;
import com.jf.cloud.product.vo.StockBillLogItemImportExcelVO;
import com.jf.cloud.product.vo.StockBillLogItemImportVO;
import com.jf.cloud.product.vo.mongo.MongoStockBillLogItemVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 出入库商品项
 *
 * @author zz
 * @date 2022-06-22 17:14:22
 */
@RestController("multishopStockBillLogItemController")
@RequestMapping("/m/stock_bill_log_item")
@Tag(name = "出入库商品项")
public class StockBillLogItemController {

    @Autowired
    private StockBillLogItemService stockBillLogItemService;

    @GetMapping("/page")
    @Operation(summary = "库存流水" , description = "库存流水")
    public ServerResponseEntity<PageVO<MongoStockBillLogItemVO>> page(@Valid PageDTO pageDTO, @ParameterObject StockBillLogDTO stockBillLogDTO) {
        stockBillLogDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<MongoStockBillLogItemVO> stockBillLogItemPage = stockBillLogItemService.page(pageDTO, stockBillLogDTO);
        return ServerResponseEntity.success(stockBillLogItemPage);
    }

    @GetMapping("/downloadModel")
    @Operation(summary = "下载导入出入库商品项模板" , description = "下载导入出入库商品项模板")
    public  ServerResponseEntity<Void> downloadModel(HttpServletResponse response, Integer type) {
        stockBillLogItemService.downloadModel(response, type);
        return ServerResponseEntity.success();
    }

    @GetMapping("/exportFlow")
    @Operation(summary = "导出库存流水" , description = "导出库存流水")
    public void exportFlow(StockBillLogDTO stockBillLogDTO, HttpServletResponse response) {
        stockBillLogDTO.setShopId(AuthUserContext.get().getTenantId());
        List<StockBillLogItemExcelVO> stockBillLogItemVOList = stockBillLogItemService.exportFlow(stockBillLogDTO, response);
        ExcelUtil.soleExcel(response, stockBillLogItemVOList, StockBillLogItemExcelVO.EXCEL_NAME, StockBillLogItemExcelVO.MERGE_ROW_INDEX, StockBillLogItemExcelVO.MERGE_COLUMN_INDEX, StockBillLogItemExcelVO.class);
    }

    @Operation(summary = "批量导入商品项" , description = "批量导入商品项")
    @PostMapping("/import_excel")
    public ServerResponseEntity<StockBillLogItemImportVO> importExcel(@RequestParam("excelFile") MultipartFile file, @RequestParam("type") Integer type){
        if(file == null) {
            throw new LuckException("网络繁忙，请稍后重试");
        }
        StockBillLogItemImportVO stockBillLogItemImportVO = new StockBillLogItemImportVO();
        try {
            // 错误信息集合
            List<String> errorList = new ArrayList<>();
            EasyExcel.read(file.getInputStream(), StockBillLogItemImportExcelVO.class, new StockBillLogItemExcelListener(stockBillLogItemService, errorList, type, stockBillLogItemImportVO)).sheet().doRead();
        } catch (IOException e) {
            throw new LuckException(e.getMessage());
        }
        return ServerResponseEntity.success(stockBillLogItemImportVO);
    }
}
